package com.iknight.mgr.core.filter;

import com.iknight.mgr.core.constant.IkgConstantCode;
import com.iknight.mgr.core.utils.IkgWebUtils;
import org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IkgRestFilter extends HttpMethodPermissionFilter {
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        // boolean isAjax = IkgWebUtils.isAjax(request);
        HttpServletRequest hreq = (HttpServletRequest) request;
        String requestPath = hreq.getRequestURI();

        String regex = "/rest/attachment/(.*)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(requestPath);
        boolean isWhiteList = false;
        if (matcher.find()) {
            isWhiteList = true;
        }

//		String yzwRegex = "/rest/yzw/(.*)";
//		Pattern yzwPattern = Pattern.compile(yzwRegex);
//		if (yzwPattern.matcher(requestPath).find()) {
//			return true;
//		}

        if (!isWhiteList && IkgWebUtils.sessionExpired()) {
            IkgWebUtils.sendJson((HttpServletResponse) response,
//					IkgWebUtils.postMsg(IkgConstantCode.CW_SESSIONEXPIRED, IkgConstantCode.SESSIONEXPIRED, false));
                    IkgWebUtils.postIkgResponse(false, IkgConstantCode.CW_SESSIONEXPIRED, IkgConstantCode.SESSIONEXPIRED));
        } else {
            return super.preHandle(request, response);
        }

        return false;
    }
}
